//
//  YPLabel.m
//  KingSelected
//
//  Created by 杨鹏 on 2023/11/6.
//

#import "YPLabel.h"

@implementation YPLabel

- (instancetype)init {
    if (self = [super init]) {
        self.verAlignment = VerticalAlgnment_center;
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.verAlignment = VerticalAlgnment_center;
    }
    return self;
}

- (void)setVerAlignment:(VerticalAlgnment)verAlignment {
    _verAlignment = verAlignment;
    [self setNeedsLayout];
}

- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
    CGRect textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];
        switch (self.verAlignment) {
            case VerticalAlgnment_top:
                textRect.origin.y = bounds.origin.y;
                break;
            case VerticalAlgnment_bottom:
                textRect.origin.y = bounds.origin.y + bounds.size.height - textRect.size.height;
                break;
            case VerticalAlgnment_center:
            default:
                textRect.origin.y = bounds.origin.y + (bounds.size.height - textRect.size.height) / 2.0;
        }
        return textRect;
}

- (void)drawTextInRect:(CGRect)rect {
    CGRect actualRect = [self textRectForBounds:rect limitedToNumberOfLines:self.numberOfLines];
    [super drawTextInRect:actualRect];
}

@end
